<?php
$sanitize_all_escapes = true;
$fake_register_globals = false;
if (!isset($_REQUEST['medico']))
    session_start();

$landingpage = "index.php?site=" . $_SESSION['site_id'];
if (isset($_SESSION['pid']) && isset($_SESSION['patient_portal_onsite'])) {
    $pid = $_SESSION['pid'];
} else {
    if (!isset($_REQUEST['medico'])) {
        session_destroy();
        header('Location: ' . $landingpage . '&w');
        exit;
    }
}
$medico = "";
$medicoIni = "";
$source_entry = "1";
$provider_id = "";
$provider_value = "";

$ignoreAuth = true;
require_once('../interface/globals.php');
//require_once("$srcdir/patient.inc");

if (isset($_REQUEST['medico']))
    $pid = $_REQUEST['set_pid'];

if (isset($_REQUEST['medico'])) {
    $provider_id = "provider_id, ";
    $provider_value = "'" . add_escape_custom($_SESSION['authUserID']) . "', ";
    $medico = "&medico=1&set_pid=" . $pid;
    $medicoIni = "?medico=1&set_pid=" . $pid;
    $source_entry = "0";
}

$res = sqlStatement("select * from patient_data where id={$pid}");
for ($iter = 0; $row = sqlFetchArray($res); $iter++)
    $result[$iter] = $row;
$iter = $result[0];
?>

<html>
    <head>

        <link rel="stylesheet" href="<?php echo $css_header; ?>" type="text/css">
        <link rel="stylesheet" href="<?php echo $web_root; ?>/library/js/redmond/jquery-ui-1.10.3.custom.min.css" type="text/css"/>
        <link rel="stylesheet" type="text/css" media="screen" href="<?php echo $GLOBALS['webroot'] ?>/patients/css/ui.jqgrid.css" />

        <link rel="stylesheet" type="text/css" href="<?php echo $GLOBALS['webroot'] ?>/patients/css/nightingale.css" >
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-1.10.0.min.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/common.js"></script>
        <link media="screen" href="css/jquery.fancybox.css?v=2.1.5" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery.fancybox.pack.js" ></script>

        <script src="<?php echo $GLOBALS['webroot'] ?>/patients/js/grid.locale-es.js" type="text/javascript"></script>
        <script src="<?php echo $GLOBALS['webroot'] ?>/patients/js/jquery.jqGrid.min.js" type="text/javascript"></script>

    </head>
    <body class="body_top">
        <FORM NAME="user_form" ID="user_form" METHOD="GET" ACTION="config_patient_save.php">

            <input type=hidden name="pid" value="<?php echo $pid; ?>" >
            <div class="borde_normal"><span class="">Configuraci&oacute;n paciente</span></div>
            <br />
            <span class="title">Agregar nuevo m&eacute;dico </span> 
            <input type="text"/>
            <a class="boton_normal" name='form_add' id='form_save' href='#' onclick='return desarrollo()'><i class="icon-save"></i> <span><?php xl('Add', 'e'); ?></span> </a>
            <br />
            <br />
            <span class="title">Permisos Individuales para m&eacute;dicos</span>     
            <br>

            <table id="list"><tr><td></td></tr></table> 
            <div id="pager"></div> 
            <br />
            <span class="title">Permisos Generales</span>     
            
            <br />
            <table>
                <tr>
                    <td>
                        <span class=text>
                            Autorizaci&oacute;n para que todos los medicos puedan ver sus datos generales.
                        </span>
                    </td>
                    <td>
                        <input type="checkbox" name="patient_general_data" <?php if ($iter["patient_general_data"]) echo " checked"; ?> />
                    </td>
                </tr>
                <tr>
                    <td>
                        <span class=text>
                            Autorizaci&oacute;n para que todos los medicos puedan ver los detalles de los datos que ha ingresado en diferentes secciones.
                        </span>
                    </td>
                    <td>
                        <input type="checkbox" name="details_auth_by_patient" <?php if ($iter["details_auth_by_patient"]) echo " checked"; ?> />
                    </td>
                </tr>
            </table>
            <br/>

            <center>
                <a class="boton_normal" name='form_save' id='form_save' href='#' onclick='return submitform()'><i class="icon-save"></i> <span><?php xl('Save', 'e'); ?></span> </a>
            </center>

        </FORM>
        <script language="JavaScript">
                    $(document).ready(function() {
                        $("#cancel").click(function() {
                            parent.$.fn.fancybox.close();
                        });
                        $("#list").jqGrid({
                            url: "config_patient_data.php?pid=<?php echo $pid; ?>",
                            autowidth: true,
                            cellEdit: true,
                            cellsubmit: 'remote',
                            cellurl: 'config_patient_update.php',
                            datatype: "xml",
                            mtype: "GET",
                            colNames: ["Medico", "Vinculado", "Favorito", "Datos generales", "Datos secciones"],
                            colModel: [
                                {name: "provider"},
                                {name: "provider_connected_patient", align: "center"},
                                {name: 'favorite', index: 'favorite',
                                    editable: true, edittype: 'checkbox', editoptions: {value: "Si:No"},
                                    formatter: "text", formatoptions: {disabled: true}, align: "center"},
                                {name: 'patient_general_data', index: 'patient_general_data',
                                    editable: true, edittype: 'checkbox', editoptions: {value: "Si:No"},
                                    formatter: "text", formatoptions: {disabled: true}, align: "center"},
                                {name: 'details_auth_by_patient', index: 'details_auth_by_patient',
                                    editable: true, edittype: 'checkbox', editoptions: {value: "Si:No"},
                                    formatter: "text", formatoptions: {disabled: true}, align: "center"}
                            ],
                            pager: "#pager",
                            rowNum: 10,
                            rowList: [10, 20, 30],
                            sortname: "id",
                            sortorder: "desc",
                            viewrecords: true,
                            gridview: true,
                            autoencode: true,
                            caption: "Mis medicos",
                            afterSubmitCell: function(serverresponse, rowid, cellname, value, iRow, iCol) {
                                $('#list').trigger("reloadGrid");
//                                alert(serverresponse.responseText);
                            },
                            beforeSubmitCell: function(rowid, celname, value, iRow, iCol) {
                                var varCellUrl = 'config_patient_update.php';
                                //alert (rowid +"/"+celname+"/"+value+"/"+iRow+"/"+iCol);
                            }
                        });
                    });

                    function submitform() {
                        //document.forms[0].submit();
                        
                        $.ajax(
                        {
                            type: "POST",
                            url: "config_patient_save.php",
                            data: $('#user_form').serialize() ,
                            cache: false,
                            success: function()
                {
                    $("#eventos").load("get_encounters.php", {'embeddedScreen': true}, function() {
                        $("#eventos").css('display', 'block');
                        $("#reportes").css('display', 'none');
                    });
                }
            });
                        
                        return true;
                    }
                    
                    function desarrollo() {
                        alert("En desarrollo...");
                        return false;
                    }
        </script>
    </BODY>
</HTML>